package com.hsq.androidproject.db;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

import com.hsq.androidproject.entity.bd_entity.Follow;
import com.hsq.androidproject.entity.bd_entity.TopicsLike;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by lenovo on 2016/12/10.
 */

public class DBHelper extends SQLiteOpenHelper {
    private static final String name = "U3Subject3.db";//数据库名称
    private static final int version = 1;//数据库版本
    //版本号:1
    private final SQLiteDatabase db;

    public DBHelper(Context context) {
        super(context, name, null, version);
        db = getReadableDatabase();
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        String sql1 = "create table search (_id not null primary key,name text not null)";
        String sql = "create table if not exists follow (_id integer not null primary key,name text not null,userName text not null,baileUrl text not null,thumb text not null," +
                "sid integer not null,rank integer not null,fansCount integer not null)";      ////收藏
        db.execSQL(sql);
        db.execSQL(sql1);
        ////////TopicsOne  个人发表信息的like
        db.execSQL("create table if not exists topicsLike (_id integer not null primary key,name text not null ,userName text not null)");

    }


    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

    }

    /*----------------------------TopicsOne  个人发表信息的like----------------------------------------------*/
    public boolean isExistT(TopicsLike topicsLike) {
        Cursor cur = db.query("topicsLike", null, "name=? and userName=? ", new String[]{String.valueOf(topicsLike.getName()), String.valueOf(topicsLike.getUserName())}, null, null, null);
        while (cur.moveToNext()) {
            String name1 = cur.getString(1);         ///////
            String userName1 = cur.getString(2);         ///////
            if (name1.equals(topicsLike.getName()) && userName1.equals(topicsLike.getUserName())) {
                return true;
            } else
                return false;
        }
        return false;
    }

    //根据id删除
    public void deleteTopicsLikeByNameAndUserName(TopicsLike topicsLike) {
        db.delete("topicsLike", "name=? and userName=?", new String[]{String.valueOf(topicsLike.getName()), String.valueOf(topicsLike.getUserName())});
    }

    public void insertTopicsLike(TopicsLike topicsLike) {
        ContentValues values = new ContentValues();
        values.put("name", topicsLike.getName());
        values.put("userName", topicsLike.getUserName());
        db.insert("topicsLike", null, values);
    }


    /*----------------------------收藏----------------------------------------------*/
    public void deleteAll() {
        db.delete("follow", null, null);
    }

    public boolean isExistF(Follow follow) {
        Cursor cur = db.query("follow", null, "name=? and userName=? ", new String[]{String.valueOf(follow.getName()), String.valueOf(follow.getUserName())}, null, null, null);
        while (cur.moveToNext()) {
            String name1 = cur.getString(1);         ///////
            String userName1 = cur.getString(2);         ///////
            if (name1.equals(follow.getName()) && userName1.equals(follow.getUserName())) {
                return true;
            } else
                return false;
        }
        return false;
    }

    //根据id删除
    public void deleteFollowByNameAndUserName(Follow follow) {
        db.delete("follow", "name=? and userName=?", new String[]{String.valueOf(follow.getName()), String.valueOf(follow.getUserName())});
    }

    public void insert(Follow collect) {
        ContentValues values = new ContentValues();
        values.put("name", collect.getName());
        values.put("userName", collect.getUserName());
        values.put("baileUrl", collect.getBaileUrl());
        values.put("thumb", collect.getThumb());
        values.put("sid", collect.getSid());
        values.put("rank", collect.getRank());
        values.put("fansCount", collect.getFansCount());
        db.insert("follow", null, values);
    }

    public List<Map<String, Object>> queryAll(String userName) {
        Cursor cursor = db.query("follow", null, "userName=?", new String[]{String.valueOf(userName)}, null, null, null);
        List<Map<String, Object>> mapList = new ArrayList<>();
        while (cursor.moveToNext()) {
            Map<String, Object> map = new HashMap<>();
            for (int i = 0; i < cursor.getColumnCount(); i++) {
                int type = cursor.getType(i);
                switch (type) {
                    case Cursor.FIELD_TYPE_STRING:
                        map.put(cursor.getColumnName(i), cursor.getString(i));
                        break;
                }
            }
            mapList.add(map);
        }
        return mapList;
    }


    public void addSearch(String name) {
        ContentValues contentValues = new ContentValues();
        contentValues.put("name", name);
        db.insert("search", null, contentValues);
    }

    public List<String> findSearch() {
        List<String> list = new ArrayList<>();
        Cursor cursor = db.query("search", null, null, null, null, null, null);
        while (cursor.moveToNext()) {
            list.add(cursor.getString(1));
        }
        return list;
    }

    public List<Map<String, Object>> queryfindSearch() {
        Cursor cursor = db.query("search", null, null, null, null, null, null);
        List<Map<String, Object>> mapList = new ArrayList<>();
        while (cursor.moveToNext()) {
            Map<String, Object> map = new HashMap<>();
            for (int i = 0; i < cursor.getColumnCount(); i++) {
                int type = cursor.getType(i);
                switch (type) {
                    case Cursor.FIELD_TYPE_STRING:
                        map.put(cursor.getColumnName(i), cursor.getString(i));
                        break;
                }
            }
            mapList.add(map);
        }
        return mapList;
    }




    /*public Follow queryByName(String name, String phone) {
        //表名 列的条件 查询条件
        Cursor cursor = db.query("follow", null, "name=? and phone=?", new String[]{String.valueOf(name), String.valueOf(phone)}, null, null, null);
        if (cursor.moveToFirst()) {
            Follow collect = new Follow();
            collect.setName(cursor.getString(cursor.getColumnIndex("name")));
            collect.setPhone(cursor.getString(cursor.getColumnIndex("phone")));
            collect.setId(cursor.getString(cursor.getColumnIndex("id")));
            collect.setGengxin(cursor.getString(cursor.getColumnIndex("gengxin")));
            collect.setGone_cover_image_url(cursor.getString(cursor.getColumnIndex("gone_cover_image_url")));
            collect.setDescription(cursor.getString(cursor.getColumnIndex("description")));
            collect.setUpdate_status(cursor.getString(cursor.getColumnIndex("update_status")));
            collect.setUpdate_day(cursor.getString(cursor.getColumnIndex("update_day")));
            collect.setNickname(cursor.getString(cursor.getColumnIndex("nickname")));
            collect.setAvatar_url(cursor.getString(cursor.getColumnIndex("avatar_url")));
            collect.setLabel_id(cursor.getString(cursor.getColumnIndex("label_id")));
            return collect;
        }
        return null;
    }*/


}
